package com.biocar.response;

import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 分页时的响应
 * @author DeSen Xu
 * @date 2021-11-22 16:08
 */
@Data
public class PageResponse<T> {

    /**
     * 数据总量
     */
    private int totalCount;

    /**
     * 总页数
     */
    private int totalPages;

    /**
     * 当前页数
     */
    private int currentPage;

    /**
     * 分页数据
     */
    private List<T> list;

    /**
     * 创建分页响应
     * @param totalCount 总数据量
     * @param max 每页最大显示行数
     * @param currentPage 当前索引数
     * @param list 分页数据
     */
    public PageResponse(int totalCount, int max, int currentPage, List<T> list) {
        this.totalCount = totalCount;
        this.currentPage = currentPage;
        this.totalPages = (int) Math.ceil((double) totalCount / max);
        this.list = list;
    }

    public static <T> PageResponse<T> emptyResponse(int totalCount, int max, int currentPage) {
        return new PageResponse<>(totalCount, max, currentPage, Collections.emptyList());
    }
}
